<?php
/**
 * page 翻页支持
 * created by 老牛
 * date 2011-07-29
 *
 */
class Page{
	
	public $amountPerPage = 0;		//每页大小
	public $totalItemAmount = 0;	//总记录数量
	public $totalPageAmount = 0;	//总页面数量
	public $currentPageNo = 0;		//当前页面，从1开始
	
	public $data = null;
	
	public $nextPageNo = null;
	public $previousPageNo = null;
	public $firstPageNo = null;
	public $lastPageNo = null;
	public $pageLink='';
	
	/**
	 * 翻页帮助类，获取翻页的内容
	 *
	 * @param unknown_type $countSql	//count sql语句
	 * @param unknown_type $sql			//查询内容的sql
	 * @param unknown_type $page		//起始页，默认1
	 * @param unknown_type $pageSize	//页面大小，默认20条记录
	 * @param unknown_type &$model 		//所在model类
	 * @return Page
	 */
public	function page($countSql, $sql, $page = 1, $pageSize = 20){
		$db = Database::getInstance();
		$this->currentPageNo	= $page;
		$this->amountPerPage	= $pageSize;
		$count					= $db->queryOne($countSql);
		$this->totalItemAmount	= $count['count'];
		$this->totalPageAmount	= (int)ceil($this->totalItemAmount / $pageSize);
		
		//页面处理
		if($this->totalPageAmount == 0){
	   		return ;
	  	}
	  	$this->firstPageNo 	= 1;
	  	if($this->currentPageNo > $this->totalPageAmount){
	   		$this->currentPageNo 	= $this->totalPageAmount;
	  	}
		if ($this->currentPageNo < $this->firstPageNo){
			$this->currentPageNo	= $this->firstPageNo;
		}
		$this->lastPageNo 	= $this->totalPageAmount;
		$this->nextPageNo 	= ($this->currentPageNo + 1) > $this->lastPageNo ? 
							$this->lastPageNo : 
							($this->currentPageNo + 1);
		$this->previousPageNo = ($this->currentPageNo - 1) < $this->firstPageNo ? 
							$this->firstPageNo : 
							($this->currentPageNo - 1);
		
		//获取数据
		$offset = ($this->currentPageNo - 1) * $this->amountPerPage;
		$sql	.= ' LIMIT '.$offset.' , '.$this->amountPerPage;
			// echo $sql;
		$this->data		= $db->query($sql);

	}
}
?>